//
//  UserInfoTableVC.m
//  DaoZiJinFu
//
//  Created by woker on 16/7/4.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "UserInfoTableVC.h"
#import "ModifyDealPassword.h"
#import "SettingLoginPassword.h"
#import "ValidatePwd.h"

@interface UserInfoTableVC () {
    NSArray *listArray;
    UISwitch *gestureSwitch;
    
    ValidatePwd *validatePwd;
}

@end

@implementation UserInfoTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"账号管理"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    
    listArray = @[@[@"头像",@"绑定手机",@"版本信息"],@[@"修改登录密码",@"修改交易密码"],@[@"手势密码",@"修改手势密码"]];
    gestureSwitch = [[UISwitch alloc] init];
    if ([userValue(HAVEGESTUREPWD) integerValue] == 1 || [userValue(HAVEGESTUREPWD) integerValue] == 3) {
        gestureSwitch.on = YES;
    } else {
        gestureSwitch.on = NO;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setGestureSuccess) name:@"setGestureSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeGestureOperation) name:@"closeGestureOperation" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingGestureOperation) name:@"settingGestureOperation" object:nil];
}

- (void)setGestureSuccess {
    gestureSwitch.on = YES;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:2]];
    if (cell == nil) {
        NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:2]];
        [self.tableView insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)logoutOperation {
    JSSHOWHUD;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/logout";
    [DZNetAPI request:request success:^(id response) {
        JSHIDEHUD;
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            [CommTools removeAllSet];
        }
        [self.navigationController popToRootViewControllerAnimated:YES];

    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        if (gestureSwitch.on == YES) {
            return 2;
        } else {
            return 1;
        }
    } else {
        if (section == 0) {
            return 3;
        }
        return 2;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 52;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section != 2) {
        return 1;
    } else {
        return 60;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        footerView.userInteractionEnabled = YES;
        UIButton *quitBtn = [[UIButton alloc] init];
        [quitBtn setTitle:@"安全退出" forState:UIControlStateNormal];
        [quitBtn setBackgroundColor:BLUE_COLOR];
        quitBtn.layer.masksToBounds = YES;
        quitBtn.layer.cornerRadius = BTN_RADIUS;
        [quitBtn addTarget:self action:@selector(logoutOperation) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:quitBtn];
        [quitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(footerView.mas_leading).offset(12);
            make.trailing.equalTo(footerView.mas_trailing).offset(-12);
            make.top.equalTo(footerView.mas_top).offset(12);
            make.height.equalTo(@44);
        }];
        
        return footerView;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userInfoCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"userInfoCell"];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    cell.textLabel.text = listArray[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = DEFAULTCOLOR;
    
    if (!indexPath.section) {
        if (!indexPath.row) {
            UIImageView *img = [[UIImageView alloc] init];
            [img sd_setImageWithURL:[NSURL URLWithString:DZUserInfo.userInfo.head_img_name] placeholderImage:[UIImage imageNamed:@"默认头像"]];
            [cell.contentView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell.contentView.mas_top).offset(8);
                make.trailing.equalTo(cell.contentView.mas_trailing).offset(-12);
                make.size.mas_equalTo(CGSizeMake(36, 36));
            }];
           // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else if (indexPath.row == 1) {
            cell.detailTextLabel.text = [DZUserInfo.userInfo.mobile stringByReplacingCharactersInRange:NSMakeRange(3, 4)withString:@"****"];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        }else{
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            cell.detailTextLabel.text = app_Version;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        }
    } else if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            [gestureSwitch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
            [cell.contentView addSubview:gestureSwitch];
            [gestureSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.contentView.mas_centerY);
                make.trailing.equalTo(cell.contentView.mas_trailing).offset(-12);
            }];
            
        } else {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            SettingLoginPassword *setPwd = [[SettingLoginPassword alloc] init];
            [self.navigationController pushViewController:setPwd animated:YES];
        } else {
            ModifyDealPassword *setDealPwd = [[ModifyDealPassword alloc] init];
            [self.navigationController pushViewController:setDealPwd animated:YES];
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            validatePwd = [ValidatePwd showView:self.view];
            validatePwd.validateType = ValidatePwdSettingGesture;
        }
    }
}

- (void)switchClick:(UISwitch *)sender {
    if (sender.isOn == YES) {
        if ([userValue(HAVEGESTUREPWD) integerValue] == 0 || [userValue(HAVEGESTUREPWD) integerValue] == 3) {
            GesturePassword *gesture = [[GesturePassword alloc] init];
            gesture.gestureType = GestureTypeSettingPassword;
            
            if ([userValue(HAVEGESTUREPWD) integerValue] == 1 || [userValue(HAVEGESTUREPWD) integerValue] == 3) {
                gestureSwitch.on = YES;
            } else {
                gestureSwitch.on = NO;
            }
            [self.navigationController pushViewController:gesture animated:YES];
        } else {
            [self.tableView reloadData];
            [theUser setObject:[NSNumber numberWithInteger:1] forKey:HAVEGESTUREPWD];
        }
    } else {
        gestureSwitch.on = !gestureSwitch.on;
        validatePwd = [ValidatePwd showView:self.view];
        validatePwd.validateType = ValidatePwdCloseGesture;
    }
}

- (void)closeGestureOperation {
    if (userValue(GESTUREPWDNUM)) {
        [theUser setObject:[NSNumber numberWithInteger:2] forKey:HAVEGESTUREPWD];
    } else {
        [theUser setObject:[NSNumber numberWithInteger:0] forKey:HAVEGESTUREPWD];
    }
    gestureSwitch.on = !gestureSwitch.on;
    [self.tableView reloadData];
}

- (void)settingGestureOperation {
    GesturePassword *gesture = [[GesturePassword alloc] init];
    gesture.gestureType = GestureTypeSettingPassword;
    [self.navigationController pushViewController:gesture animated:YES];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

@end
